<!--
 * @Description:维修建议 maintenanceAdvice
 * @Autor: 刘诗文
 * @Date: 2020-12-17 16:24:32
 * @LastEditors: 马毅
 * @LastEditTime: 2021-06-29 16:18:11
-->
<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" :labelWidth="105" @filterChange="changeHandle"></TopFilter>
    <FilterTable
      ref="labourTable"
      columnsRef="repari_repairReception_customer_repairSuggest_labour"
      height="auto"
      :columns="repairLabourColumns"
      :datakey="'rows'"
      :dataSource="labourDataSource"
      :onSyncTableData="handleSyncTableData"
      :isExportExcel="true"
      exportFileName="项目建议.xlsx"
      :onRowSelectChange="
        list => {
          this.selectedLabourList = list;
        }
      "
      :onColumnsChange="columns => (this.repairLabourColumns = columns)"
    >
    </FilterTable>
    <FilterTable
      ref="partTable"
      columnsRef="repari_repairReception_customer_repairSuggest_part"
      height="auto"
      :columns="repairPartColumns"
      :datakey="'rows'"
      :dataSource="partDataSource"
      :onSyncTableData="handleSyncTableData"
      :isExportExcel="true"
      exportFileName="配件建议.xlsx"
      :onRowSelectChange="
        list => {
          this.selectedPartList = list;
        }
      "
      :onColumnsChange="columns => (this.repairPartColumns = columns)"
    >
    </FilterTable>

    <div class="dialogBottmView">
      <el-button @click="handleCancel">取消</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { confirmAction, notifyAction, findFormItem, findTableItem } from '@/utils';

import { queryRepairSuggestCustomerCare } from '@/api/customerRepair/customerCare/adviceRepair';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import dictCode from '@/utils/dictCode';

export default {
  mixins: [dictionary],
  data() {
    this.selectedLabourList = []; // 选中的项目列表
    this.selectedPartList = []; // 选中的配件列表
    return {
      topFilterList: this.createTopFilterList(),
      repairLabourColumns: this.createRepairLabourColumns(),
      repairPartColumns: this.createRepairPartColumns(),

      labourDataSource: [], // 项目数据源
      partDataSource: [], // 配件数据源
      syncTableData: [] // 同步的table数据
    };
  },
  mounted() {
    // 获取服务顾问
    this.getServiceAdvisor();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '发动机号',
          fieldName: 'engineNo',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车主',
          fieldName: 'ownerName',
          initialValue: ''
        },
        {
          type: 'RANGE_DATE',
          label: '建议日期',
          fieldName: 'suggestDateBigin|suggestDateEnd',
          rules: [{ required: true, message: '请选择建议日期', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '车主地址',
          fieldName: 'address',
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '服务顾问',
          fieldName: 'serviceAdvisor',
          itemList: []
        }
      ];
    },
    createRepairLabourColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '项目代码',
          dataIndex: 'LABOUR_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '项目名称',
          dataIndex: 'LABOUR_NAME',
          width: 200,
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '工时',
          dataIndex: 'STD_LABOUR_HOUR',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '工时费',
          dataIndex: 'LABOUR_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '不修原因',
          dataIndex: 'REASON',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8070'),
          dictItems: this.createDictList('8070')
        },
        {
          title: '建议日期',
          dataIndex: 'SUGGEST_DATE',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        // {
        //   title: '操作员',
        //   dataIndex: 'CREATED_BY',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: 'VIN',
          dataIndex: 'VIN',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发动机号',
          dataIndex: 'ENGINE_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车主',
          dataIndex: 'OWNER_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系电话',
          dataIndex: 'MOBILE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车主地址',
          dataIndex: 'ADDRESS',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    createRepairPartColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '配件数量',
          dataIndex: 'PART_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单价',
          dataIndex: 'PART_SALES_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2
        },
        {
          title: '不修原因',
          dataIndex: 'REASON',
          sorter: true
        },
        {
          title: '建议日期',
          dataIndex: 'SUGGEST_DATE',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        // {
        //   title: '维修项目代码',
        //   dataIndex: 'LABOUR_CODE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: []
        // },
        // {
        //   title: '仓库',
        //   dataIndex: 'STORAGE_CODE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '操作员',
        //   dataIndex: 'CREATED_BY',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: 'VIN',
          dataIndex: 'VIN',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发动机号',
          dataIndex: 'ENGINE_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
        // {
        //   title: '车主地址',
        //   dataIndex: 'address',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // }
      ];
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    },
    async changeHandle(val) {
      this.$refs.labourTable.START_LOADING();
      this.$refs.partTable.START_LOADING();
      try {
        let res = await queryRepairSuggestCustomerCare(val);
        if (res.resultCode == 200) {
          // 处理部分数据
          res.data.partList.forEach(item => {
            item.PART_QUANTITY = item.QUANTITY;
          });
          this.labourDataSource = res.data.labourList;
          this.partDataSource = res.data.partList;
        }
      } catch (err) {
        console.log(err, '查询列表失败');
      } finally {
        this.$refs.labourTable.STOP_LOADING();
        this.$refs.partTable.STOP_LOADING();
      }
    },
    // 点击取消
    handleCancel() {
      this.$emit('close');
    },
    // 服务顾问
    async getServiceAdvisor() {
      let roleParm = {
        role: dictCode.SERVICE_CONSULTANT
      };
      const res = await searchRoleByCode(roleParm);
      this.repairPartColumns.find(x => x.dataIndex == 'SERVICE_ADVISOR').dictItems = this.repairLabourColumns.find(x => x.dataIndex == 'SERVICE_ADVISOR').dictItems = this.topFilterList.find(
        item => item.fieldName === 'serviceAdvisor'
      ).itemList = res.data.map(x => ({ value: x.userId + '', text: x.employeeName }));
    }
  }
};
</script>

<style scoped></style>
